package com.jck.data.task;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.jck.data.util.ChkUtil;
import com.jck.data.util.QuartzFactoryUtil;

public class StartEtlInitTaskUtil {
	public static void runInitFile() {
			String jarWholePath = StartEtlInitTaskUtil.class.getProtectionDomain().getCodeSource().getLocation().getFile();
			try {
			
		        // 去掉路径信息中的协议名"file:"
		        int pos = jarWholePath.indexOf("file:");
		        if (pos > -1) {
		        	jarWholePath = jarWholePath.substring(pos + 5);
		        }
				jarWholePath = java.net.URLDecoder.decode(jarWholePath, "UTF-8");
				String jarPath = "";
				if(jarWholePath.contains(".war") || jarWholePath.contains(".jar")) {
					jarPath = new File(jarWholePath).getParentFile().getPath();
				} else {
					jarPath = new File(jarWholePath).getParentFile().getPath();
				}
	           System.out.println(jarPath);
				File dir = new File(jarPath+File.separator+"etlFile/");
				if(!dir.exists()){
					jarWholePath = Thread.currentThread().getContextClassLoader().getResource("")+"etlFile/";
					jarWholePath = jarWholePath.indexOf("file:")!=-1?jarWholePath.substring(jarWholePath.indexOf("file:") + 5):jarWholePath;
					dir=  new File(jarWholePath);
				}
				
//				String[] suff= {"kjb","ktr"}; 
				String[] suff= {"kjb"}; 
				File[] files = FileUtils.convertFileCollectionToFileArray(FileUtils.listFiles(dir, suff, true));
				for(File f:files) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("transName", f.getAbsolutePath());
					System.out.println(f.getAbsolutePath());
					String fileName = null;
					if(f.getName()  != null&&!f.getName().equals("")){
						fileName = f.getName();
						fileName=fileName.substring(fileName.lastIndexOf(File.separator)+1);
					}
					if( fileName  != null) {
							QuartzFactoryUtil.getInstance().addJobByCalendar(fileName,fileName,
									DataSyncTask.class, 10L, map);
					} 
				}
	
			} catch (Exception e) {
				System.out.println("e:"+e);
			}
		}
		
}

